package BJHZRZ.User;

import javax.swing.*;
import java.util.*;

public class DbConnect
{

	private System.Data.SqlClient.SqlConnection DbConnection;
	public tangible.Event<TimeOutEnvent> TimeOutEvent = new tangible.Event<TimeOutEnvent>();
	private SqlConnectionStringBuilder stringBuilder;





	public DbConnect(String connectionString)
	{

		stringBuilder = new SqlConnectionStringBuilder(connectionString);
		   DbConnection = new SqlConnection(stringBuilder.toString());
		DbConnection.Open();
//C# TO JAVA CONVERTER TODO TASK: Java has no equivalent to C#-style event wireups:
		DbConnection.InfoMessage += new SqlInfoMessageEventHandler(DbConnection_InfoMessage);
	}

	private void DbConnection_InfoMessage(Object sender, SqlInfoMessageEventArgs e)
	{

	}

	public final int ExecuteSql(String sql)
	{
		SqlCommand execute = new SqlCommand(sql, DbConnection);
		return execute.ExecuteNonQuery();
	}

	public final void Dispose()
	{
		DbConnection.Dispose();

	}

	public final SqlDataReader ExecuteSqlwithRe(String sql)
	{
		SqlCommand execute = new SqlCommand(sql, DbConnection);
		return execute.ExecuteReader();
	}

	/** 
	 获取strSql对应的SqlDataAdapter
	 
	 @param strSql SQL语句
	 @return SqlDataAdapter
	*/
	public final SqlDataAdapter GetDataAdapter(String strSql)
	{
		SqlCommand command = new SqlCommand(strSql, DbConnection);
		SqlDataAdapter da = new SqlDataAdapter(command);
		return da;
	}

	/** 
	 获取和strSql对应的DataSet
	 
	 @param strSql SQL语句
	 @return DataSet
	*/
	public final DataSet GetDataSet(String strSql)
	{

		SqlDataAdapter da = GetDataAdapter(strSql);
		DataSet ds = new DataSet();
		try
		{
			da.Fill(ds);
		}
		catch (SqlException ex)
		{
			 if (ex.Class == 11 || ex.Class == 20)
			 {
				if (this.TimeOutEvent != null)
				{
					SqlErrorCommand returnCommand = SqlErrorCommand.forValue(0);
					for (TimeOutEnvent listener : TimeOutEvent.listeners())
					{
						listener.invoke(strSql, out returnCommand);
					}
					if (returnCommand == SqlErrorCommand.ReTry)
					{
						this.stringBuilder.ConnectTimeout += 2000;

						this.DbConnection.Dispose();
						this.DbConnection = new SqlConnection(this.stringBuilder.toString());
						 this.DbConnection.Open();

						return GetDataSet(strSql);
					}
				}
			 }
		}

		catch (RuntimeException ex)
		{
			   this.Err(ex);
		}
		return ds;
	}
	/** 
	 数据库有关的错误处理
	 
	 @param ex 异常
	*/
	protected final void Err(RuntimeException ex)
	{
		if (ex instanceof SqlException)
		{
			SqlException Ex = (SqlException)ex;
			  JOptionPane.showConfirmDialog(null, new Form(), "Class:" + Ex.Class + "ex.Message:" + Ex.getMessage() + "ex.Number" + Ex.Number.toString() + "ex.Soure" + Ex.Server + Ex.Source + Ex.State + "other" + Ex.toString(), JOptionPane.DEFAULT_OPTION);
		}
		else
		{
			JOptionPane.showConfirmDialog(null, new Form(), ex.toString(), JOptionPane.DEFAULT_OPTION);
		}
	}

  //  public SqlR

	public final void sqlgo(ArrayList<String> sql)
	{
		SqlTransaction sqltransaction = this.DbConnection.BeginTransaction();

		for (String Sql1 : sql)
		{
			SqlCommand sqlcommand = new SqlCommand(Sql1,this.DbConnection, sqltransaction);
			sqlcommand.ExecuteNonQuery();

		}
		sqltransaction.Commit();
	}
}